/**
 * 
 */
package com.kgcx.schedule.rabbitmq.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.kgcx.common.client.schedule.pojo.ScheduleRecord;
import com.kgcx.common.rabbitmq.consts.ExchangeConst;
import com.kgcx.common.rabbitmq.consts.RoutingKeyConst;
import com.kgcx.core.utils.JsonUtil;

/**
 * @author 袁进勇
 *
 */
@Component
public class OrderScheduledProducer {
    private static final Logger LOG = LoggerFactory.getLogger(OrderScheduledProducer.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void addSuccessful(ScheduleRecord record) {
        LOG.info(String.format("订单[%s]，发送调度记录新增成功的MQ消息。", record.getOrder_no()));
        rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_ADD_SUCCESSFUL,
                JsonUtil.toString(record));
    }

    public void addFailed(ScheduleRecord record) {
        LOG.info(String.format("订单[%s]，发送调度记录新增失败的MQ消息。", record.getOrder_no()));
        rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_ADD_FAILED,
                JsonUtil.toString(record));
    }

    public void scheduleSuccessful(ScheduleRecord record) {
        LOG.info(String.format("订单[%s]，发送调度记录派车成功的MQ消息。", record.getOrder_no()));
        rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_SCHEDULE_SUCCESSFUL,
                JsonUtil.toString(record));
    }

    public void scheduleFailed(ScheduleRecord record) {
        LOG.info(String.format("订单[%s]，发送调度记录派车失败的MQ消息。", record.getOrder_no()));
        rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_SCHEDULE_FAILED,
                JsonUtil.toString(record));
    }

    public void unlockDriver(ScheduleRecord record) {
        LOG.info(String.format("订单[%s]，发送延时解锁司机的MQ消息。", record.getOrder_no()));
        rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_UNLOCK_DRIVER,
                JsonUtil.toString(record));
    }

    /**
     * 发送改派司机的MQ消息，注意：record中只有order_no和driver_id，且driver_id为旧司机的ID
     * 
     * @param record
     */
    public void changeDriver(ScheduleRecord record) {
        LOG.info(String.format("订单[%s]，发送改派司机的MQ消息。", record.getOrder_no()));
        rabbitTemplate.convertAndSend(ExchangeConst.SCHEDULE, RoutingKeyConst.SCHEDULE_CHANGE_DRIVER,
                JsonUtil.toString(record));
    }
}
